#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 200005
#define mod 998244353
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
const int INF=1e9;
int qpow(int a,int b){
	int res=1;
	while(b){
		if(b&1) res=res*a%mod;
		a=a*a%mod;b>>=1;
	}
	return res;
}
/*
int fac[N],ifac[N];
int C(int n,int m){
	if(n<m||n<0||m<0) return 0;
	return fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
void init(){
	fac[0]=1;
	for(int i=1;i<N;i++) fac[i]=fac[i-1]*i%mod;
	ifac[N-1]=qpow(fac[N-1],mod-2);
	for(int i=N-2;i>=0;i--) ifac[i]=ifac[i+1]*(i+1)%mod;
}*/
int T,a[N],t[N],cnt,dp[N],n;
signed main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		cnt=0;int tag=0,ans=0;
		cin>>n;
		for(int i=1;i<=n;i++) cin>>a[i],t[++cnt]=a[i];
		sort(t+1,t+cnt+1);cnt=unique(t+1,t+cnt+1)-t-1;
		for(int i=1;i<=n;i++) a[i]=lower_bound(t+1,t+cnt+1,a[i])-t;
		for(int i=1;i<=cnt;i++) dp[i]=-INF*INF;
		if(cnt==1){
			cout<<(n-1)*t[1]<<endl;
			continue;
		}int sum=0;
		int mx1=-INF*INF;
		//mx1 yi zhi shi dui de
		for(int i=2;i<=n;i++){
			if(a[i-1]==a[i-2]) sum=sum+t[a[i-1]];
			int mx=max(sum,max(dp[a[i]]+tag+t[a[i]],mx1));
			if(a[i-1]==a[i]) tag=tag+t[a[i]],mx1=mx1+t[a[i]];
			if(dp[a[i-1]]+tag<mx){
				dp[a[i-1]]=mx-tag;
				mx1=max(mx1,mx);
			}
		}
		if(a[n]==a[n-1]) sum=sum+t[a[n]];ans=sum;
		for(int i=1;i<=cnt;i++) ans=max(ans,dp[i]+tag);
		cout<<ans<<endl;
	}
	return 0;
}